var url;
var projectId = null;
var detailId = null;

$(function() {
	$(".datepicker").datepicker({
		showButtonPanel: true
	});
	$('.datepicker').datepicker("option", "dateFormat", "yy-mm" );
	$('.datepicker').datepicker('option', 'changeMonth', true);
	
	//初始化====================================
	$("#type").buttonset();
	$("#dataGird").flexigrid({
		url: "/project/newprosummary/selectForPage.json",
		colModel : [
		    {display: "ID", name : "id", width : 90, sortable : true,hide: true},
	        {display: "项目名称", name : "name", width : 400, sortable : true},
	        {display: "项目编码", name : "code", width : 90, sortable : true},
	        {display: "承接部门", name : "deptName", width : 90, sortable : true},
			{display: "合同额(元)", name : "contractValue", width : 90, sortable : true,process:formatCost},
			{display: "工资/奖金系数(%)", name : "wageCoefficient", width : 110, sortable : true, hide: true,process:formatPercent},
			{display: "类型", name : "type", width : 90, sortable : true,process:formatType},
			{display: "统计期", name : "gmtRefund", width : 90, sortable : true},
			{display: "PID", name : "proid", width : 90, sortable : true, hide: true},
			{display: "外业地勘(元)", name : "geosurveyCost", width : 90, sortable : true,process:formatCost},
			{display: "外业测量(元)", name : "surveyCost", width : 90, sortable : true,process:formatCost},
			{display: "内业费用(元)", name : "designCost", width : 90, sortable : true,process:formatCost},
			{display: "外业其他(元)", name : "otherCost", width : 90, sortable : true,process:formatCost},
			{display: "OTHERDETAIL", name : "otherDetail", width : 90, sortable : true, hide: true},
			{display: "小计", name : "outsideCount", width : 90, sortable : true,process:formatCost},
			{display: "外业承包(元)", name : "outsideCost", width : 90, sortable : true,process:formatCost},
			{display: "会务费用(元)", name : "conferenceCost", width : 90, sortable : true,process:formatCost},
			{display: "分院产值(元)", name : "branchValue", width : 90, sortable : true,process:formatCost},
			{display: "小计", name : "otherCount", width : 90, sortable : true,process:formatCost},
			{display: "合计", name : "totalCount", width : 90, sortable : true,process:formatCost},
			{display: "已完成(%)", name : "completedRate", width : 90, sortable : true,process:formatPercent},
			{display: "预(核)算完成额(元)", name : "budgetcalcCplValue", width : 90, sortable : true,process:formatCost},
			{display: "创建时间", name : "gmtCreate", width : 120, sortable : true, hide: true},
			{display: "创建人", name : "creator", width : 120, sortable : true, hide: true},
			{display: "修改时间", name : "gmtCreate", width : 120, sortable : true, hide: true},
			{display: "修改人", name : "modifier", width : 120, sortable : true, hide: true}
			]
    }); 
	
	$('#projectDialog').dialog({
		resizable: false,
		autoOpen: false,
		width: 650,
		height: 500,
		modal: true
	});
	
	
	//事件监控====================================
	//重置表单
	$("#clearButton").bind("click",function(){
		MZ.reset("searchForm");
	});
	
	//查询数据
	$("#searchButton").bind("click",function(){
		MZ.searchGird("searchForm","dataGird");
	});
	
	//EXPORT数据
	$("#exportA").bind("click",function(){
		//MZ.showMsg("未完善的功能，请等待...");
		var name = $("#name").val();
		var dept = $("#deptName").val();
		var type = $("#type").val();
		var st = $("#startTime").val();
		var et = $("#endTime").val();
		if(name == ""){
			name = "-1";
		}
		if(dept == ""){
			dept = "-1";
		}
		if(type == ""){
			type = "-1";
		}
		if(st == ""){
			st = "-1";
		}
		if(et == ""){
			et = "-1";
		}
		window.location.href = "/project/newprosummary/exportSummary/"+encodeURI(encodeURI(name))+"/"+encodeURI(encodeURI(dept))+"/"+type+"/"+st+"/"+et;
	});
	
	//取消
	$("#cancelBtn").bind("click", function(){
		MZ.closeDialog("projectDialog");
	});
	//重置
	$("#resetBtn").bind("click", function(){
		MZ.reset("projectForm");
		$('#type1').click();
	});
	
});

function onSelectRow(tr){
	//
}



function formatCost(tdDiv, pid){
	var cost = $(tdDiv).text();
	if(cost == "0"){
		return "";
	}
	return MZ.toDecimal(cost/MZ.CURRENCY_PRECISION , MZ.CURRENCY_ROUND);
}
function formatPercent(tdDiv, pid){
	var val = $(tdDiv).text();
	if(val != ""){
		//val += "%";
	}
	return val;
}

function formatType(tdDiv, pid){
	var type = $(tdDiv).text();
	if (type==1){
		return "预算";
	}else if(type=2){
		return "核算";
	}else{
		return "未知";
	}
}

function readonlyForm(formId, flag){
	if(flag){
		$("#"+formId+" input").attr("readonly",true);
	}else{
		$("#"+formId+" input").attr("readonly",false);
	}
}

function emptyOtherDetail(){
	var liSize = $("#othercostField li").length;
	for(i = 2; i<liSize; i++){
		$('#othercostLI'+i).remove();
	}
}